!function (factory) {
    'use strict';
    'function' === typeof define && define.amd ? define(['jquery', 'tree', 'ace'], factory) : factory(jQuery);
}(function ($) {

    var Tree = function (ele, opt) {

        this.defaults = $.extend({}, $.fn.ace_tree.defaults, {
            loadingHTML: '<div class="tree-loading"><i class="ace-icon fa fa-refresh fa-spin blue"></i></div>',
            'open-icon': 'ace-icon fa fa-folder-open',
            'close-icon': 'ace-icon fa fa-folder',
            'itemSelect': true,
            'folderSelect': true,
            'multiSelect': false,
            'selected-icon': null,
            'unselected-icon': null,
            // 'selected-icon' : 'ace-icon fa fa-check',
            // 'unselected-icon' : 'ace-icon fa fa-times',
            'folder-open-icon': 'ace-icon tree-plus',
            'folder-close-icon': 'ace-icon tree-minus'
        });
        this.options = $.extend({}, this.defaults, opt || {});
    };

    $.fn.xumfTree = $.fn.xumf_tree = function (options) {

        if ('string' === typeof options) {
            var treeObj = $(this).data('fu.tree'),
                method = treeObj[options],
                args = $.makeArray(arguments).slice(1);
            if (!method) throw "tree - 没有这样的方法: " + options;
            return method.apply(treeObj, args);
        }

        return this.each(function () {
            var tree = new Tree(this, options);
            // 初始化
            $(this).ace_tree(tree.options);
        });
    };

    // 定义暴露format函数
    $.fn.xumfTree.format = function (txt) {
        return '<strong>' + txt + '</strong>';
    };
    /**
     * 获取当前tree选中item，当且仅当选中一条时返回数据，否则返回null
     * @type {function(*): null}
     */
    $.fn.xumfTree.getSelectedItem = $.fn.xumf_tree.getSelectedItem = function ($tree) {
        // 当前tree选择的code
        var items = $tree.tree('selectedItems');
        return items.length === 1 ? items[0] : null;
    }
});